package com.BookmanageSystem.controller;

import com.BookmanageSystem.entity.Book;
import com.BookmanageSystem.entity.BookType;
import com.BookmanageSystem.entity.User;
import com.BookmanageSystem.service.BookService;
import com.BookmanageSystem.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    BookService bookService;
    @Resource
    UserService userService;

    //获取全部图书信息
    @RequestMapping("/getBookList")
    @ResponseBody
    public List<Book> getBookList(){
        List<Book> bookList=bookService.getBookList();
        return bookList;
    }
    //获取图书类型
    @RequestMapping("/getBookTypeList")
    @ResponseBody
    public List<BookType> getTypeList(){
        List<BookType> typeList=bookService.getTypeList();
        System.out.println(typeList);
        return typeList;
    }

    @RequestMapping("/getTypeListByName")
    @ResponseBody
    public List<BookType> getTypeListByName(HttpServletRequest request){
        System.out.println(bookService.getTypeListByName(request.getParameter("bookName")));
        return bookService.getTypeListByName(request.getParameter("bookName"));
    }

    @RequestMapping("/getBookListByName")
    @ResponseBody
    public List<Book> getBookListByName(HttpServletRequest request) {
        System.out.println(request.getParameter("bookName"));
        List<Book> bookList = bookService.getBookListByName(request.getParameter("bookName"));
        System.out.println(bookList);
        return bookList;
    }
    @RequestMapping("/deleteBookById")
    @ResponseBody
    public String deleteBookById(HttpServletRequest request){
        return bookService.deleteBookById(request.getParameter("bookId"));
    }

    @RequestMapping("/updateBook")
    public String updateBook(HttpServletRequest request) {
        Book book=new Book();
        book.setId(Integer.parseInt(request.getParameter("bookId")));
        book.setBookName(request.getParameter("bookName"));
        book.setTypeName(request.getParameter("bookType"));
        Integer i=bookService.updateBook(book);
        request.setAttribute("updateState",i);
        System.out.println(book);
        return "tsgl";
    }

    @RequestMapping("/updateType")
    @ResponseBody
    public String updateType(HttpServletRequest request){
        BookType type=new BookType();
        type.setId(Integer.parseInt(request.getParameter("typeId")));
        type.setTypeName(request.getParameter("typeName"));
        System.out.println(type);
        return bookService.updateType(type).toString();
    }

    @RequestMapping("/outBook")
    @ResponseBody
    public Integer outBook(HttpServletRequest request){
        Book book=new Book();
        book.setId(Integer.parseInt(request.getParameter("bookId")));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String userName=request.getParameter("userName");
        System.out.println(userName);
        User user = userService.getUserByName(userName);
        System.out.println(user);
        if(user!=null){
            book.setUserId(user.getId());
            if(request.getParameter("outDate")!=""&&request.getParameter("inDate")!="") {
                java.util.Date d = null;
                try {

                    d = sdf.parse(request.getParameter("outDate"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                java.sql.Date date = new java.sql.Date(d.getTime());
                book.setOutDate(date);
                java.util.Date d2 = null;
                try {
                    d2 = sdf.parse(request.getParameter("inDate"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                java.sql.Date date2 = new java.sql.Date(d2.getTime());
                book.setInDate(date2);
            }else {
                return 0;
            }
            book.setState(0);
            return bookService.outBook(book);
        }
        else {
            return 0;
        }
    }

    @RequestMapping("/inBook")
    @ResponseBody
    public Integer inBook(HttpServletRequest request){
        return bookService.inBook(request.getParameter("bookId"));
    }

    @RequestMapping("/getBookById")
    @ResponseBody
    public Book getBookById(HttpServletRequest request){
        System.out.println( bookService.getBookById(request.getParameter("bookId")));
        return bookService.getBookById(request.getParameter("bookId"));
    }

    @RequestMapping("/toUpdateBookPage")
    public String toUpdateBookPage(HttpServletRequest request){
        request.setAttribute("bookId",request.getParameter("bookId"));
        Book book=bookService.getBookById(request.getParameter("bookId"));
        System.out.println(book);
        String bookName=book.getBookName();
        String bookType=book.getTypeName();
        String author=book.getAuthor();
        Date outDate=book.getOutDate();
        request.setAttribute("bookName",bookName);
        request.setAttribute("bookType",bookType);
        request.setAttribute("author",author);
        return "updatebook";
    }

    @RequestMapping("/toUpdateTypePage")
    public String toUpdateTypePage(HttpServletRequest request){
        request.setAttribute("typeId",request.getParameter("typeId"));
        BookType bookType=bookService.getTypeById(request.getParameter("typeId"));
        System.out.println(bookType);
        String typeName=bookType.getTypeName();
        request.setAttribute("typeName",typeName);
        return "updatetype";
    }

    @RequestMapping("/toAddTypePage")
    public String toAddTypePage(HttpServletRequest request){
        return "addType";
    }

    @RequestMapping("/toTsgl")
    public String toTsgl(HttpServletRequest request){
        return "tsgl";
    }

    @RequestMapping("/toAddBookPage")
    public String toAddBookPage(HttpServletRequest request){
        return "addbook";
    }

    @RequestMapping("/toOutBookPage")
    public String toOutBookPage(HttpServletRequest request){
        request.setAttribute("bookId",request.getParameter("bookId"));
        Book book=bookService.getBookById(request.getParameter("bookId"));
        System.out.println(book);
        String bookName=book.getBookName();
        String bookType=book.getTypeName();
        String author=book.getAuthor();
        request.setAttribute("bookName",bookName);
        request.setAttribute("bookType",bookType);
        request.setAttribute("author",author);
        return "outbook";
    }

    @RequestMapping("/addBook")
    @ResponseBody
    public String addBook(HttpServletRequest request){
        Book book=new Book();
        book.setBookName(request.getParameter("bookName"));
        book.setTypeName(request.getParameter("bookType"));
        book.setAuthor(request.getParameter("author"));
        book.setState(1);
        Integer state=bookService.addBook(book);
        System.out.println(state);
        return state.toString();
    }

    @RequestMapping("/addType")
    @ResponseBody
    public String addType(HttpServletRequest request){
        Integer state=bookService.addType(request.getParameter("bookType"));
        return state.toString();
    }

    @RequestMapping("/deleteTypeById")
    @ResponseBody
    public String deleteTypeById(HttpServletRequest request){
        Integer state=bookService.deleteTypeById(request.getParameter("typeId"));
        System.out.println(request.getParameter("typeId"));
        System.out.println(state);
        return state.toString();
    }

}
